package com.hunterai.controller;

import com.hunterai.dto.AuthResponse;
import com.hunterai.dto.LoginRequest;
import com.hunterai.dto.RegisterRequest;
import com.hunterai.service.AuthService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hunterai.dto.ReturnData;

/**
 * Controller for authentication operations
 */
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final AuthService authService;

    @Autowired
    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    /**
     * Endpoint for user registration
     *
     * @param request the registration request containing user details
     * @return the authentication response with token and user info
     */
    @PostMapping("/register")
    public ResponseEntity<AuthResponse> register(@RequestBody RegisterRequest request) {
        AuthResponse response = authService.register(request);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * Endpoint for user login
     *
     * @param request the login request containing credentials
     * @return the authentication response with token and user info
     */
    @PostMapping("/login")
    public ReturnData<AuthResponse> login(@RequestBody LoginRequest request) {
        AuthResponse response = authService.login(request);
        return ReturnData.success(response);
    }

    @GetMapping("/unauthorized")
    public ResponseEntity<Map<String, String>> unauthorized() {
        Map<String, String> response = new HashMap<>();
        response.put("error", "未授权访问");
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
    }
} 